package com.example.blog.web;


import com.example.blog.pojo.blog;
import com.example.blog.pojo.tag;
import com.example.blog.pojo.type;
import com.example.blog.pojo.user;
import com.example.blog.service.blogService;
import com.example.blog.service.tagService;
import com.example.blog.service.typeService;
import com.example.blog.service.userService;
import com.example.blog.util.MD5;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class adminController {

    @Autowired
    private userService userService;

    @GetMapping({"/admin"})
    public String loginPage(){

        return "admin/login";
    }

    /**
     * 用户登录校验：
     *  使用MD5加密：
     *      传入密码并进行加密，于数据库中加密后的密码进行校验
     *  如果session中没有user就跳转到登录界面
     *  账号密码错误也跳回登录界面
     *
     * @param username
     * @param password
     * @param session
     * @param attributes
     * @return
     */
    @PostMapping("/admin/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session, RedirectAttributes attributes){
        password = MD5.getMD5(password);
        user user = userService.cheekUser(username,password);
        if (user != null){
            user.setPassword(null);
            session.setAttribute("user",user);
            return "redirect:/admin/index";
        }else {
            attributes.addFlashAttribute("message","账号密码有误");
            return "redirect:/admin";
        }

    }

    /**
     * 用户退出登录：
     *  从session中移除user
     * @param session
     * @return
     */
    @GetMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/admin";
    }

    @GetMapping({"/admin/index"})
    public String index(){

        return "admin/index";
    }



}
